package com.proactive.webapp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.proactive.webapp.dao.CourseDAO;
import com.proactive.webapp.dao.DesignCategoryDAO;
import com.proactive.webapp.dao.DesignCommentDAO;
import com.proactive.webapp.dao.DesignDAO;
import com.proactive.webapp.dao.DesignTagDAO;
import com.proactive.webapp.dao.MemberDAO;
import com.proactive.webapp.dao.MenuDAO;
import com.proactive.webapp.entity.Course;
import com.proactive.webapp.entity.Design;
import com.proactive.webapp.entity.DesignCategory;
import com.proactive.webapp.entity.DesignComment;
import com.proactive.webapp.entity.DesignTag;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.Menu;
import com.proactive.webapp.util.Category;
import com.proactive.webapp.util.MenuUtil;
import com.proactive.webapp.util.PageCount;

import org.apache.commons.lang.StringEscapeUtils;
@Controller
public class DesignController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");

	@RequestMapping(value = "/design", method = RequestMethod.GET)
	public ModelAndView getDesignList(HttpServletRequest request, HttpSession session) {
		//import DAO
		DesignCategoryDAO designCategoryDAO = (DesignCategoryDAO) context.getBean("designCategoryDAO");
		DesignDAO designDAO = (DesignDAO) context.getBean("designDAO");
		ModelAndView model = new ModelAndView();
		
		
		//get data from request
		String type = request.getParameter("t")==null?"new":request.getParameter("t");
		String category1Req = request.getParameter("c1")==null?"":request.getParameter("c1");
		int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
		
		//define id
		int category1ID = category1Req.equals("")?0:Integer.parseInt(category1Req);
		
		//set pager
		int pageSize = 16;
		int count = designDAO.countTotal(category1ID);
		String pageUrl = "design";
		String pageEndUrl1 = "&t="+type+"&c1="+category1ID;
		PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, pageEndUrl1);
		pageCount.calculate();
		
		//set category
		String pageEndUrl2 = "&c1="+category1ID;
		Category category = new Category(type, category1ID, 0, pageUrl, pageEndUrl2);
		category.set1();
		
		//get design by condition
		List<Design> designList = new ArrayList<Design>();
		List<DesignCategory> category1List = new ArrayList<DesignCategory>();
		category1List = designCategoryDAO.getList();
		if(category1ID != 0)
			designList = designDAO.getListByCategory(type, category1ID, pageCount.getStart(), pageSize);
		else
			designList = designDAO.getListByType(type, pageCount.getStart(), pageSize);

		//set category1 list(class,url,name...)
		DesignCategory category1Index = new DesignCategory();
		category1Index.setId(0);
		category1Index.setName("全部分類");
		category1Index.setClassString(category1ID==0?"selected":"");
		category1Index.setUrl(pageUrl+"?t="+category.getType());
		category1List.add(0, category1Index);
		for(int i=0; i<category1List.size(); i++){
			DesignCategory category1 = category1List.get(i);
			if(category1.getId() == category1ID){
				category.setName1(category1.getName());
				category1List.get(i).setClassString("selected");
			}
			String url = pageUrl+"?t="+category.getType()+"&c1="+category1.getId();
			category1List.get(i).setUrl(url);
				
		}

		List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 2);
		
		//set model
		model.addObject("page", pageCount);
		model.addObject("designList", designList);
		model.addObject("category1", category1List);
		model.addObject("category", category);
		model.addObject("menuList", menuList);
		

		//check login
		Member loginMember = (Member)session.getAttribute("loginMember");
		model.setViewName(loginMember!=null?"design1":"design2");
		
		return model;
	}
	
	@RequestMapping(value = "/practice", method = RequestMethod.GET)
	public ModelAndView getPracticeList(HttpServletRequest request, HttpSession session, int id) {
		//import DAO
		DesignDAO designDAO = (DesignDAO) context.getBean("designDAO");
		CourseDAO courseDAO = (CourseDAO) context.getBean("courseDAO");
		ModelAndView model = new ModelAndView();
		
		//get data from request
		int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
		
		//set pager
		int pageSize = 16;
		int count = designDAO.countPracticeTotal(id);
		String pageUrl = "practice";
		String pageEndUrl1 = "&id="+id;
		PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, pageEndUrl1);
		pageCount.calculate();
		

		
		//get design
		List<Design> designList = new ArrayList<Design>();
		designList = designDAO.getPracticeList(id, pageCount.getStart(), pageSize);
		Course course = new Course();
		course.setId(id);
		course = courseDAO.get(course);
		
		List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 0);
		
		//set model
		model.addObject("page", pageCount);
		model.addObject("course", course);
		model.addObject("designList", designList);
		model.addObject("menuList", menuList);
		

		//check login
		Member loginMember = (Member)session.getAttribute("loginMember");
		model.setViewName(loginMember!=null?"practice1":"practice2");
		
		return model;
	}
	
	@RequestMapping(value = "/showDesign", method = RequestMethod.GET)
	public ModelAndView getDesign(HttpServletRequest request, HttpSession session) {
		
		ModelAndView model = new ModelAndView();
		if(request.getParameter("id")!=null && !request.getParameter("id").equals("")){
			DesignDAO designDAO = (DesignDAO) context.getBean("designDAO");
			MemberDAO memberDAO = (MemberDAO) context.getBean("memberDAO");
			DesignTagDAO designTagDAO = (DesignTagDAO) context.getBean("designTagDAO");
			DesignCommentDAO designCommentDAO = (DesignCommentDAO) context.getBean("designCommentDAO");
			
			//check login
			Member loginMember = (Member)session.getAttribute("loginMember");
			
			int designID = Integer.parseInt(request.getParameter("id"));
			int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
			
			Design design = new Design();
			design.setId(designID);
			Member member = new Member();
			List<DesignComment> commentList = new ArrayList<DesignComment>(); 
			List<DesignTag> tagList = new ArrayList<DesignTag>();
			List<Design> designTop3List = new ArrayList<Design>();
			

			//set pager
			int pageSize = 5;
			int countComment = designCommentDAO.countTotal(design.getId());
			String pageUrl = "showDesign";
			String pageEndUrl1 = "&id="+design.getId()+"#commentArea";
			PageCount pageCount = new PageCount(pageNow, pageSize, countComment, pageUrl, pageEndUrl1);
			pageCount.calculate();
			String loginAccount = loginMember!=null?loginMember.getAccount():"";
			if(loginMember != null){
				design = designDAO.getByLogin(design, loginMember.getAccount());
				member.setAccount(design.getAccount());
				member = memberDAO.getByLogin(member, loginMember.getAccount());
				if(design.getStatus()!=1){
					if(design.getStatus() != 3)
						model.setViewName(design.getAccount().equals(loginAccount)?"designDetail1":"redirect:/design");
					else
						model.setViewName("redirect:/design");
				}
				else
					model.setViewName("designDetail1");
			}
			else{
				design = designDAO.get(design);
				member.setAccount(design.getAccount());
				member = memberDAO.get(member);
				model.setViewName(design.getStatus()==1?"designDetail2":"redirect:/design");
			}
			
			
			
			design.setViewCount(design.getViewCount()+1);
			designDAO.updateViewCount(design);
			commentList = designCommentDAO.getList(loginAccount, designID, pageCount.getStart(), pageSize);
			tagList = designTagDAO.getListByDesignID(designID);
			designTop3List = designDAO.getListByCategoryTop3(design.getCategoryID(), design.getId());

			int count = designDAO.countTotal(design.getCategoryID());
			design.setMoreFlag((designTop3List.size()==2||count>3)?true:false);
			design.setMoreString(design.isMoreFlag()?"更多":"沒有更多");
			design.setMoreUrl(design.isMoreFlag()?design.getCategoryUrl():"javascript:;");
			for(int i=0; i<commentList.size(); i++){
				DesignComment designComment = commentList.get(i);
				List<DesignComment> replyCommentList = designCommentDAO.getReplyList(loginAccount, designComment.getId());
				commentList.get(i).setReplyCommentList(replyCommentList);
				if(designComment.getAccount().equals(loginAccount))
					commentList.get(i).setDeleteFlag(true);
				
				for(int j=0; j<replyCommentList.size(); j++){
					DesignComment replyComment = replyCommentList.get(j);
					if(replyComment.getAccount().equals(loginAccount))
						commentList.get(i).getReplyCommentList().get(j).setDeleteFlag(true);
				}
			}
			
			if(designTop3List.size() == 2)
				designTop3List.remove(1);
			for(int i=0; i<designTop3List.size(); i++){
				designTop3List.get(i).setClassString(i==1?"modify2":"modify");					
			}

			design.setContent(StringEscapeUtils.unescapeHtml(design.getContent()));
			
			List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 2);
			
			model.addObject("design", design);
			model.addObject("member", member);
			model.addObject("commentList", commentList);
			model.addObject("designList", designTop3List);
			model.addObject("tagList", tagList);
			model.addObject("page", pageCount);
			model.addObject("menuList", menuList);
		}
		else
			model.setViewName("redirec:/design");
		return model;
	}
}
